using System;
using System.Collections.Generic;
using System.Text;
using Mud.Common.Communication;
using Mud.Common.Messages;
using Mud.Common;
using System.Threading;
using System.Net;
using Mud.Common.Physical;


namespace Mud.LoadTest
{

	class Program
	{

		private static int _threads;


		static void Main(string[] args) {

			System.Threading.Thread.CurrentThread.Name = "Client";
            Logger.AddSink(new ConsoleLogSink((LogType.LogSeverity.Information | LogType.LogSeverity.Error | LogType.LogSeverity.Warning)));
            Logger.AddSink(new FileLogSink());
            string input = string.Empty;

            //TestMessages.TestMessage();
            //Console.ReadLine();
            //return;

			DateTime lastRead = DateTime.Now;
			DateTime currentRead;

			Console.Out.WriteLine("Client load test for Mud game server.");
			Console.Out.WriteLine("Commands:");
			Console.Out.WriteLine("   'e' - exit");
			Console.Out.WriteLine("   '+' - add one connection");
			Console.Out.WriteLine("   '++' - add ten connections");
            Console.Out.WriteLine("   '+++' - add fifty connections");

			while (input != "e") {
				Console.Out.Write("> ");
				input = Console.In.ReadLine();
				currentRead = DateTime.Now;
				TimeSpan duration = currentRead.Subtract(lastRead);

				if (input == "+") {
					StartServerConnection();
				}
				if (input == "++") {
					StartServerConnection();
					StartServerConnection();
					StartServerConnection();
					StartServerConnection();
					StartServerConnection();
					StartServerConnection();
					StartServerConnection();
					StartServerConnection();
					StartServerConnection();
					StartServerConnection();
				}
                if (input == "+++")
                {
                    for(int i = 0; i < 50; i++) 
                        StartServerConnection();
                }
            }

		}

		private static void StartServerConnection() {
			Thread thread = new Thread(new ThreadStart(ServerConnection));
			thread.IsBackground = true;
			thread.Start();
			_threads++;
		}

		private static void ServerConnection() {

			Client.Client client = new Mud.Client.Client(new EmptyShell());


			if (!client.Connect("localhost", 6001, "dc_" + Thread.CurrentThread.ManagedThreadId.ToString(), ""))
			{
				Logger.Log(LogType.Error, "LoadTest", "Unable to connect.");
				client.Disconnect();
				return;
			}

			Logger.Log(LogType.Info, "dc_" + Thread.CurrentThread.ManagedThreadId.ToString(), "Client {0}: connected", Thread.CurrentThread.ManagedThreadId);
			Random r = new Random();
			if (!client.StartPlaying())
			{
				Logger.Log(LogType.Error, "dc_" + Thread.CurrentThread.ManagedThreadId.ToString(), "Unable to start playing.");
				client.Disconnect();
				return;
			}

			int counter = 1;
			while (client.Connected)
			{
				client.Tick();
				Thread.Sleep(100);
				if ((counter % 100) == 0)
				{
					int x = r.Next(0, client.PlayerArea.Size.Width -1);
					int y = r.Next(0, client.PlayerArea.Size.Height-1);
                    client.RequestMoveCharacter(new Location(x, y), MovementType.Walking);
				}
				counter++;
			}
		}


		internal class EmptyShell : Client.IClientShell
		{
			#region IClientShell Members

			public Mud.Client.Creature SelectCreature(IList<Mud.Client.Creature> creatures)
			{
				return creatures[0];
			}

			#endregion
		}

	}
}
